package bpel.instructions;

import bpel.BPELProcess;
import bpel.types.BPELLiteral;
import bpel.variables.BPELComplexVariable;
import bpel.variables.BPELMessageVariable;
import bpel.variables.BPELStandardVariable;

/**
 * 
 * @author Maurice & Topia
 * 
 */
public class BPELTo {

	private BPELLiteral literal;
	private String variable;
	private String part;
	private String query;

	public BPELTo(BPELLiteral literal, String variable, String part, String query) {
		this.literal = literal;
		this.variable = variable;
		this.part = part;
		this.query = query;
	}

	public void receive(String value, BPELProcess process) {
		if (isLiteral()) {
			System.err.println("(BPELTo) Can't assign a literal");
		} else {
			if (mustQuery()) {
				if (part != null) {
					BPELMessageVariable msgVar = (BPELMessageVariable) process.getVariable(variable);
					msgVar.set(part, query, value);
				} else {
					BPELComplexVariable cplxVar = (BPELComplexVariable) process.getVariable(variable);
					cplxVar.set(query, value);
				}
			} else {
				BPELStandardVariable stdVar = (BPELStandardVariable) process.getVariable(variable);
				stdVar.setValue(value);
			}
		}
	}
	
	public boolean mustQuery(){
		return query != null;
	}

	public String getVariableName() {
		return variable;
	}

	public boolean isLiteral() {
		return literal != null;
	}

	public BPELLiteral getLiteral() {
		return literal;
	}

	@Override
	public String toString() {
		if (literal != null) {
			return literal.toString();
		} else {
			return "(var=" + variable + ", part=" + part + ", query=" + query + ")";
		}
	}
}
